﻿// <copyright file="Default.aspx.cs" company="NTalent">
// Copyright (c) 2010, NTalent, Inc. All rights reserved
// </copyright>
// <author>卢俊坤</author>
// <author>王彦博 符茂松</author>

using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Net.Ntalent.Action50.Core.Controls;
using Net.Ntalent.Action50.DataAccess;
using Net.Ntalent.Action50.Core.Service;
using Net.Ntalent.Action50.Core.Util;
using Net.Ntalent.Action50.Core.Dao;
using System.Collections.Generic;

/// <summary>
/// 首页
/// </summary>
///<author>王彦博</author> 
/// <copyright>
/// Copyright (c) 2010, NTalent, Co. Ltd. All rights reserved.
/// </copyright>
/// <version>1.0.0</version>
public partial class Default : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session[Constants.SESSION_KEY_ADMIN_NAME] == null)
        {
            Li_Admin.Visible = false;
            Li_Login.Visible = true;
        }else
        {
            Li_Admin.Visible = true;
            Li_Login.Visible = false;
        }

        popupCmd.Text = string.Empty;
        if(IsPostBack)
        {
            popupCmd.Text = string.Empty;
        }

        ReadOnlyService readOnlyService = new ReadOnlyService();

        //对投票栏执行数据绑定操作。
        if (ViewState["VotePage"] == null)
        {
            ViewState["VotePage"] = 1;
        }
        int votePage = Convert.ToInt32(ViewState["VotePage"]);
        Repeater_columnVotes.DataSource = readOnlyService.RetrieveRankingList(votePage, 10);
        Repeater_columnVotes.DataBind();

        //对留言栏执行数据绑定操作。
        Repeater_columnLeavewords.DataSource = readOnlyService.RetrieveLastWords(10);
        Repeater_columnLeavewords.DataBind();

        //为主题内容执行数据绑定操作。
        Repeater_columnContent.DataSource = readOnlyService.RetrieveLastNews(5);
        Repeater_columnContent.DataBind();

        //对进度条的显示情况进行操作。
        long totalEffectPeople = readOnlyService.TotalEffectPeopleCount();
        TotalEffectPeopleCount.InnerText = totalEffectPeople.ToString();
        long feetCount = totalEffectPeople / 50000;
        List<string> feet = new List<string>();
        for (long i = 0; i < feetCount && i < 17; i++)
        {
            string className = "guageFoot_" + ((i % 2 == 0) ? "01" : "02");
            feet.Add(className);
        }
        Repeater_guageNow.DataSource = feet;
        Repeater_guageNow.DataBind();

    }

    protected void Repeater_columnVotes_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //进行投票操作
        if (e.CommandName == "Vote")
        {
            long actionId = Convert.ToInt64(e.CommandArgument);
            var voteService = new VoteService();
            string ipAdress = Request.UserHostAddress;
            VoteActionResult result = voteService.Vote(ipAdress, actionId);
            if (result == VoteActionResult.SUCCESS)
            {
                Message.Text = "Vote Successfully!";
                popupCmd.Text = "<script type='text/javascript'>$('.popup').css('display', 'block');$('#alpha').css('display', 'block');</script>";
            }
            if (result == VoteActionResult.FAIL)
            {
                Message.Text = "Vote Failed！";
                popupCmd.Text = "<script type='text/javascript'>$('.popup').css('display', 'block');$('#alpha').css('display', 'block');</script>";
            }
            if (result == VoteActionResult.ACTIONVOTEALEADY)
            {
                Message.Text = "Sorry, only one chance to vote for each action!";
                popupCmd.Text = "<script type='text/javascript'>$('.popup').css('display', 'block');$('#alpha').css('display', 'block');</script>";
            }
            if (result == VoteActionResult.VOTEALREADY)
            {
                Message.Text = "Sorry, you have only 5 chances to vote!";
                popupCmd.Text = "<script type='text/javascript'>$('.popup').css('display', 'block');$('#alpha').css('display', 'block');</script>";
            }
            return;
        }

        var readOnlyService = new ReadOnlyService();

        //取得当前所在页的页码
        int votePage = Convert.ToInt32(ViewState["VotePage"]);

        //点击投票栏的Next按钮时
        if (e.CommandName == "Next")
        {
            //已经到最后一页了，则返回
            if (Helper.isLastVotePage(Context, votePage))
            {
                return;
            }

            //尚未到达最后一页
            Repeater_columnVotes.DataSource = readOnlyService.RetrieveRankingList(votePage + 1, 10);
            ViewState["VotePage"] = Convert.ToInt64(ViewState["VotePage"]) + 1;
            Repeater_columnVotes.DataBind();
            return;
        }

        //点击投票栏的Previous按钮时
        if (e.CommandName == "Previous")
        {
            //已经是第一页了，则返回
            if (votePage <= 1)
            {
                return;
            }

            //当前页不是第一页
            Repeater_columnVotes.DataSource = readOnlyService.RetrieveRankingList(votePage - 1, 10);
            Repeater_columnVotes.DataBind();
            return;
        }
    }

    /// <summary>
    /// 为每个新闻拉取第一张图片并显示
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Repeater_columnContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //得到该item中用来显示照片的Panel
        PlaceHolder photos = (PlaceHolder)e.Item.FindControl("PlaceHolder_Photos");

        //得到该news
        T_NEWS_AND_LOGS news = ((T_NEWS_AND_LOGS)e.Item.DataItem);

        //为news中的第一个照片生成HtmlImage对象并添加到panel中
        foreach (string name in Helper.GetImageNamesOfNews(news))
        {
            Panel panel = new Panel();
            panel.CssClass = "postPhoto";
            HtmlImage image = new HtmlImage();
            image.Src = name;
            panel.Controls.Add(image);
            photos.Controls.Add(panel);
        }
    }

    protected void Repeater_columnVotes_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //为前三名添加特殊标记
        T_ACTIONS action = (T_ACTIONS)e.Item.DataItem;
        Panel panel = (Panel)e.Item.FindControl("Panel_imageRank");
        if (action != null && panel != null)
        {
            if (Helper.GetTheOrder(action) == 1)
            {
                panel.Attributes["class"] = "img1st rank";
            }
            if (Helper.GetTheOrder(action) == 2)
            {
                panel.Attributes["class"] = "img2nd rank";
            }
            if (Helper.GetTheOrder(action) == 3)
            {
                panel.Attributes["class"] = "img3th rank";
            }
        }
    }

    protected void Repeater_columnContent_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    }

    protected void Repeater_columnLeavewords_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //跳转到留言所在的新闻或者日志
        if (e.CommandName == "gotoWords")
        {
            NewsAndLogsDao newsAndLogsDao = new NewsAndLogsDao();
            T_NEWS_AND_LOGS newsAndLogs = newsAndLogsDao.Retrieve(Convert.ToInt64(e.CommandArgument));
            if (newsAndLogs.LOGS_TYPE == (int)NewsAndLogsType.LOGS)
            {
                Response.Redirect("action_Team_Logs.aspx?logsId=" + newsAndLogs.Id);
            }
            if (newsAndLogs.LOGS_TYPE == (int)NewsAndLogsType.NEWS)
            {
                Response.Redirect("action_Team_News.aspx?newsId=" + newsAndLogs.Id);
            }
        }
    }
    protected void LinkButton_popup_Click(object sender, EventArgs e)
    {
        popupCmd.Text = string.Empty;
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        popupCmd.Text = string.Empty;
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        new AuthenticationService().LogOut();
        Response.Redirect(Request.UrlReferrer.ToString());
    }
}
